//
//  Rainbow.m
//  UI05_rainbowHomework
//
//  Created by dllo on 16/6/12.
//  Copyright © 2016年 司昌宝. All rights reserved.
//

#import "Rainbow.h"

@implementation Rainbow
//自定义初始化
-(instancetype)initWithFrame:(CGRect)frame withCount:(NSInteger)count{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatViewWithCount:count];
    }
    return self;
}
//根据count创建视图层,count为层数
-(void)creatViewWithCount:(NSInteger)count{
    for (NSInteger i = count; i > 0; i--) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0,self.frame.size.width / (float)count * i, self.frame.size.height / (float)count * i)];
        view.center = CGPointMake(self.center.x - self.frame.origin.x, self.center.y - self.frame.origin.y);
        view.backgroundColor = [UIColor colorWithRed:arc4random() % 255 / 255.0 green:arc4random() % 255 / 255.0 blue:arc4random() % 255 / 255.0 alpha:1.0];
        [self addSubview:view];
        [view release];
    }
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(changeColor) userInfo:nil repeats:YES];

}

//传递每一层的颜色
-(void)changeColor{
    NSArray *viewArray = self.subviews;
    UIColor *temp = [[UIColor alloc] init];
    UIView *view = [viewArray objectAtIndex:0];
    temp = view.backgroundColor;
    for (NSInteger i = 0; i < viewArray.count - 1;i++){
        UIView *nowView = [viewArray objectAtIndex:i];
        UIView *nextView = [viewArray objectAtIndex:i + 1];
        nowView.backgroundColor = nextView.backgroundColor;
    }
    UIView *maxView = [viewArray objectAtIndex:viewArray.count - 1];
    maxView.backgroundColor = temp;
}

@end
